package com.jh.fcsm.util.io;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * java压缩成zip
 *
 * @author szx
 * 创建时间：2015年1月14日
 */
public class FileZip {
    /**
     * @param inputFileName 你要压缩的文件夹(整个完整路径)
     * @param zipFileName   压缩后的文件(整个完整路径)
     * @throws Exception
     */
    public static Boolean zip(String inputFileName, String zipFileName) throws Exception {
        zip(zipFileName, new File(inputFileName));
        return true;
    }

    private static void zip(String zipFileName, File inputFile) throws Exception {
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
        zip(out, inputFile, "");
        out.flush();
        out.close();
    }

    private static void zip(ZipOutputStream out, File f, String base) throws Exception {
        if (f.isDirectory()) {
            File[] fl = f.listFiles();
            out.putNextEntry(new ZipEntry(base + "/"));
            base = base.length() == 0 ? "" : base + "/";
            for (int i = 0; i < fl.length; i++) {
                zip(out, fl[i], base + fl[i].getName());
            }
        } else {
            out.putNextEntry(new ZipEntry(base));
            FileInputStream in = null;
            try {
                in = new FileInputStream(f);
                int b;
                while ((b = in.read()) != -1) {
                    out.write(b);
                }
            } catch (Exception e) {
                throw e;
            } finally {
                if (in != null) {
                    in.close();
                }
            }

        }
    }
    //szx   把服务器上的文件下载道本地

    /**
     * @param fileStr
     * @param zipUul
     * @param realName
     * @return void * @throws
     * @throws IOException
     */
    public static void getDownloadFile(String fileStr, String zipUul, String realName) throws IOException {
        File file = new File(fileStr);
        InputStream in = new FileInputStream(file);
//		FileUpload.copyFile(in, fileConfig.getFilePath() + Constant.FILE_REVIEW_PATH, realName);//把文件上传到文件目录里面
        FileUpload.copyFile(in, zipUul, realName);
    }

    public static void main(String[] temp) {
        try {
//			 zip("E:\\ftl","E:\\test.zip");//你要压缩的文件夹      和  压缩后的文件 
//			 FileUtil.delFile("E:\\zipTest");
//			 DelAllFile.delFolder("E:\\zipSc\\ZZZ");
        } catch (Exception ex) {
        }
    }

}

//=====================文件压缩=========================
/*//把文件压缩成zip
File zipFile = new File("E:/demo.zip");
//定义输入文件流
InputStream input = new FileInputStream(file);
//定义压缩输出流	
ZipOutputStream zipOut = null;
//实例化压缩输出流,并制定压缩文件的输出路径  就是E盘下,名字叫 demo.zip
zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
zipOut.putNextEntry(new ZipEntry(file.getName()));
//设置注释
zipOut.setComment("www.demo.com");
int temp = 0;
while((temp = input.read()) != -1) {
	zipOut.write(temp);	
}		
input.close();
zipOut.close();*/
//==============================================
